<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/16 14:08
 * Desc: 发票管理--必须要进行登录检查
 */
class Controller_Pc_Api_Standard_Receipt extends Controller_Pc_Api_Base
{
    private $_mid;

    public function before()
    {
        parent::before();
        // 检查用户是否有效
        $mid = intval($this->request_body->content->mid);
        $check_member = Model_Api_Standard_Pub::check_member_effective($mid);
        if($check_member['status'] == false)
        {
            $this->send_datagrams($this->client_info['id'], $check_member, $this->client_info['secret_key']);
        }

        $this->_mid = $mid;
    }

    // 获取我的发票列表
    public function action_get_receipt_list()
    {
        $pagesize  = intval($this->request_body->content->pagesize);
        $startSize = intval($this->request_body->content->startSize);
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $types = Common::remove_xss($this->request_body->content->types);

        $res = Model_Api_Standard_Receipt::get_receipt_list($this->_mid,$keyword,$types,$startSize,$pagesize);
        $result = array('status'=> true, 'msg'=> '获取成功','list'=> $res['list'],'completed'=> $res['completed']);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 保存发票
     */
    public function action_save_receipt()
    {

        // 检查发票是否可以进行保存
        $receiptId = intval($this->request_body->content->receiptId);
        $invoice_model = ORM::factory('member_invoice',$receiptId);
        if($invoice_model->loaded() && $invoice_model->memberid!=$this->_mid)
        {
            $result = array('status'=>false,'msg'=>'权限错误');
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        // 0 普通个人 1 普通公司 2 增值发票
        $mtype   = intval($this->request_body->content->mtype);
        $subtype = intval($this->request_body->content->subtype);
        $type = $subtype == 1 ? 2 : $mtype;

        // 纳税人识别号
        $taxpayer_number = Common::remove_xss($this->request_body->content->taxpayer_number);
        if($type!=0 && !empty($taxpayer_number))
        {
            $id = $receiptId ? $receiptId : 0;
            $num = DB::query(Database::SELECT, "select count(*) as num from sline_member_invoice where id!='{$id}' and taxpayer_number='{$taxpayer_number}' ")->execute()->get('num');
            if($num>0)
            {
                $result = array('status'=>false,'msg'=>'纳税人识别号重复');
                $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
            }
        }

        $title = Common::remove_xss($this->request_body->content->title);
        $taxpayer_address = Common::remove_xss($this->request_body->content->taxpayer_address);
        $taxpayer_phone = Common::remove_xss($this->request_body->content->taxpayer_phone);
        $bank_branch = Common::remove_xss($this->request_body->content->bank_branch);
        $bank_account = Common::remove_xss($this->request_body->content->bank_account);

        $invoice_model->type = $type;
        $invoice_model->memberid = $this->_mid;
        $invoice_model->title = $title;
        $invoice_model->taxpayer_number  = $type == 1 || $type==2? $taxpayer_number : '';
        $invoice_model->taxpayer_address = $type == 2 ? $taxpayer_address : '';
        $invoice_model->taxpayer_phone   = $type == 2 ? $taxpayer_phone : '';
        $invoice_model->bank_branch      = $type == 2 ? $bank_branch : '';
        $invoice_model->bank_account     = $type == 2 ? $bank_account : '';
        $invoice_model->save();
        if($invoice_model->saved())
        {
            $result = array('status'=>true,'msg'=>'保存成功','id'=>$invoice_model->id);
        }
        else
        {
            $result = array('status'=>true,'msg'=>'保存失败');
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    /**
     * @function 获取我的发票详情
     */
    public function action_get_receipt_info()
    {
        // 检查发票是否可以获取
        $receiptId = intval($this->request_body->content->receiptId);
        if(!$receiptId)
        {
            $result = array('status'=>false,'msg'=>'提交信息有误');
        }
        else
        {
            // 返回发票数据
            $info = DB::select()->from('member_invoice')->where('id','=',$receiptId)->and_where('memberid','=',$this->_mid)->execute()->current();
            if(!$info)
            {
                $result = array('status'=>false,'msg'=>'获取发票信息错误');
            }
            else
            {
                $result = array('status'=>true,'msg'=>'获取成功','info'=> $info);
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 删除发票
     */
    public function action_del_receipt()
    {

        // 检查发票是否可以获取
        $receiptId = intval($this->request_body->content->receiptId);
        if(!$receiptId)
        {
            $result = array('status'=>false,'msg'=>'提交信息有误');
        }
        else
        {
            $invoice_model = ORM::factory('member_invoice')->where('id','=',$receiptId)->and_where('memberid','=',$this->_mid)->find();
            if(!$invoice_model->loaded())
            {
                $result = array('status'=>false,'msg'=>'发票信息不存在');
            }
            else
            {
                $invoice_model->delete();
                $result = array('status'=>true,'msg'=>'删除成功');
            }

        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取会员下单的发票选择数据
     */
    public function action_get_order_receipt()
    {

        $typeid = intval($this->request_body->content->typeid);
        if(!$typeid)
        {
            $result = array('status'=>false,'msg'=>'提交信息有误');
        }
        else
        {
            // 检查该产品类型是否开启发票
            $open        = $GLOBALS['cfg_invoice_open_'.$typeid]; // 是否开启发票填写
            $contents    = $GLOBALS['cfg_invoice_content_'.$typeid]; // 发票明细
            $description = $GLOBALS['cfg_invoice_des_'.$typeid]; // 发票说明
            $types       = $GLOBALS['cfg_invoice_type_'.$typeid]; // 发票类型

            $contents = explode(',',$contents);
            $types    = explode(',',$types);

            $result = array(
                'isopen'=> $open,
                'status'=> true,
                'contents'=> $contents,
                'types'=> $types,
                'description'=> $description
            );
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取订单发票信息
     */
    public function action_get_order_receipt_Info()
    {
        $orderid = intval($this->request_body->content->orderid);
        if(!$orderid)
        {
            $result = array('status'=> false, 'msg'=> '提交信息有误');
        }
        else
        {
            $sql = "SELECT a.* FROM sline_member_order_bill AS a INNER JOIN sline_member_order AS b ON a.orderid=b.id AND b.memberid={$this->_mid} WHERE a.orderid={$orderid} ";
            $info = DB::query(1,$sql)->execute()->current();
            if($info)
            {
                $result = array('status'=> true, 'msg'=> '获取成功','info'=> $info);
            }
            else
            {
                $result = array('status'=> false, 'msg'=> '提交信息有误');
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取产品是否发票填写
     */
    public function action_get_receipt_open_status()
    {
        $typeid = intval($this->request_body->content->typeid);
        if(!$typeid)
        {
            $result = array('status'=> false, 'msg'=> '提交信息有误');
        }
        else
        {
            $open   = $GLOBALS['cfg_invoice_open_'.$typeid]; // 是否开启发票填写
            $result = array('status'=> true, 'msg'=> '获取成功','isOpenReceipt'=> $open);

        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

}